package com.bjtu.ptsite.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import java.util.Properties;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class EmailUtil {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}") // 确保从配置读取
    private String from;

    public void sendVerificationEmail(String to, String code, int request) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message);

            if (request == 0) {
                helper.setFrom(from);
                helper.setTo(to);
                helper.setSubject("[PT SITE] **✦♡✧ 验证码小可爱来袭 ✧♡✦**​");
                helper.setText("(*´∀`)~♥ 您的专属验证码是：​🌸" + code + "🌸，5分钟内有效( •̀ ω •́ )✧。感谢您使用我们的网站喵 ฅ^•ﻌ•^ฅ。\n\n\n\n欢迎骚扰客服22301028@bjtu.edu.cn٩(ˊᗜˋ*)و ✨");

                mailSender.send(message);
                log.info("邮件发送成功：{}", to);
            }
            else if (request == 1) {
                helper.setFrom(from);
                helper.setTo(to);
                helper.setSubject("[PT SITE] 哼~这是你要的验证码，可别手抖输错了哦​");
                helper.setText("杂鱼该不会是忘了自己的密码了吧~还要找本小姐帮你修改~验证码给你吧："+ code +
                "\n\n改完密码记得感谢本小姐发验证码哦~"+
                "\n遇到问题？​​ → 联系本小姐3442989823@qq.com\r\n" + 
                "(但人家可没保证一定会帮你呢~嘻嘻)");

                mailSender.send(message);
                log.info("邮件发送成功：{}", to);
            }
        } catch (MessagingException e) {
            log.error("邮件发送失败", e);
            throw new RuntimeException("邮件服务异常");
        }
    }
}
